package com.lemon.components.ant.docmaker;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.lemon.components.ant.docmaker.dto.EnumParams;
import com.lemon.components.ant.docmaker.dto.EnumReq;
import com.lemon.components.ant.docmaker.dto.EnumRsp;
import com.lemon.components.ant.swagger.makeui.AntdFromSwaggerMaker;
import com.lemon.components.ant.util.TemplateUtil;

import lombok.experimental.UtilityClass;

@UtilityClass
public class EnumMaker {
	
	private static final double CELLZERO = (double)0;

	public static void main(String[] args) throws Exception {
		EnumReq req = EnumReq.builder()
			.enumCodeIndex(0)
			.enumMessageIndex(1)
			.totalRows(12)
			.comment("交易状态")
			.codeIsNumber(false)
			.clazzName("bankPayStateEnum")
			.packageName("com.liaofarm.liaoken.integration.yinsheng.eqitong.trade.enums")
			.excelPath("D:\\params.xlsx")
			.savePath("D:\\workspace\\liaoken\\liaoken-integration\\src\\main\\java\\com\\liaofarm\\liaoken\\integration\\yinsheng\\eqitong\\trade\\enums\\")
			.build();
		EnumMaker.make(req);
	}
	public void make(EnumReq req) throws Exception {
		FileInputStream fis = new FileInputStream(req.getExcelPath());
		try (XSSFWorkbook workbook = new XSSFWorkbook(fis)) {
			List<EnumRsp> enumList = new ArrayList<EnumRsp>();
			XSSFSheet sheet = workbook.getSheetAt(0);
			int totalRow;
			if(req.getTotalRows()==null || req.getTotalRows()==0) {
				totalRow = sheet.getLastRowNum();
			} else {
				totalRow = req.getTotalRows();
			}
			
			System.out.println("共计"+totalRow+"行。");
			int i = 0;
			if(req.isIgnoreHeader())
				i = 1;
			for (; i < totalRow; i++) {
				XSSFRow row = sheet.getRow(i);
				String enumCodeString=null;
				Integer enumCodeNumber=null;
				if(req.getCodeIsNumber()) {
					enumCodeNumber = getInteger(row.getCell(req.getEnumCodeIndex()));
				} else {
					enumCodeString = getString(row.getCell(req.getEnumCodeIndex()));
				}
				EnumRsp enumRsp = new EnumRsp();
				enumRsp.setEnumCodeInteger(enumCodeNumber);
				enumRsp.setEnumCodeString(enumCodeString);
				enumRsp.setEnumMessage(getString(row.getCell(req.getEnumMessageIndex())));
				enumList.add(enumRsp);
			}
			EnumParams enumParams = new EnumParams();
			enumParams.setReq(req);
			enumParams.setRsp(enumList);
			
			TemplateUtil.getInstance(AntdFromSwaggerMaker.class.getResource("/code/").getFile()).build(
					"/vue/EnumTemplate.ftl", 
					req.getSavePath()+StringUtils.capitalize(req.getClazzName())+".java",
					enumParams);
			workbook.close();
			
			workbook.close();
		}
		fis.close();
	}
	
public String getString(XSSFCell cell) {
		
		if(cell == null)
			return StringUtils.EMPTY;
		
		switch (cell.getCellTypeEnum()) {
			case NUMERIC: 
				double d = cell.getNumericCellValue();
				return String.valueOf(d);
			case STRING: 
				return cell.getStringCellValue().trim();
			case BLANK: 
				return "";
			case BOOLEAN: 
				return String.valueOf(cell.getBooleanCellValue());
			default:
				return StringUtils.EMPTY;
		}
	}
	
	public Double getNumeric(XSSFCell cell) {
		if(cell == null)
			return CELLZERO;
		switch (cell.getCellTypeEnum()) {
			case NUMERIC: 
				return cell.getNumericCellValue();
			case STRING: 
				try {
					String tmp = cell.getStringCellValue();
					return Double.valueOf(tmp);
				} catch (Exception e) {
					return CELLZERO;
				}
			case BLANK: 
				return CELLZERO;
			default:
				return CELLZERO;
		}
	}
	
	public Integer getInteger(XSSFCell cell) {
		Double tmp = getNumeric(cell);
		if(tmp == null)
			return 0;
		else
			return getNumeric(cell).intValue();
	}
}
